<?php

namespace app\modules\controllers;


use app\modules\models\Admin;
use Codeception\Module\REST;
use Yii;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;
use yii\web\Controller;

class ManageController extends Controller
{
    public $layout = 'layout1';

    public function actionMailchangepass()
    {
        $this->layout = false;
        $adminuser    = Yii::$app->request->get('adminuser');
        $timestamp    = Yii::$app->request->get('timestamp');
        $token        = Yii::$app->request->get('token');

        $model   = new Admin();
        $myToken = $model->createToken($adminuser, $timestamp);
        if ($myToken != $token) {
            return $this->redirect(['public/login']);
        }

        if (time() - $timestamp > 600) {
            return $this->redirect(['public/login']);
        }

        if (Yii::$app->request->isPost) {
            if ($model->changePass(Yii::$app->request->post())) {
                Yii::$app->session->setFlash('info', '密码修改成功');
//                return $this->redirect(['public/login']);
            }
        }

        $model->adminuser = $adminuser;
        return $this->render('mailchangepass', [
            'model' => $model,
        ]);
    }

    public function actionManagers()
    {
        $this->layout = 'layout1';

        $model = Admin::find();
        $count = $model->count();
        $pager = new Pagination([
            'totalCount' => $count,
            'pageSize'   => 2,
        ]);

        $managers = $model->offset($pager->offset)->limit($pager->limit)->all();
        return $this->render('managers', [
            'managers' => $managers,
            'pager'    => $pager,
        ]);
    }

    public function actionReg()
    {
        $model = new Admin();

        if (Yii::$app->request->isPost) {
            if ($model->addAdmin(Yii::$app->request->post())) {
                Yii::$app->session->setFlash('info', '添加成功');
            } else {
                Yii::$app->session->setFlash('info', '添加失败');
            }
        }

        $model->adminpass = '';
        $model->repass    = '';
        return $this->render('reg', [
            'model' => $model,
        ]);
    }

    public function actionDel($id = false)
    {
        if (false == $id) {
            return $this->goBack();
        }

        $model = new Admin();
        if ($model->deleteAll(['adminid' => $id])) {
            Yii::$app->session->setFlash('info', '删除成功');
            return $this->redirect(['manage/managers']);
        }
    }

    public function actionChangemail()
    {
        $model = Admin::find()->where(['adminuser' => Yii::$app->session['admin']['adminuser']])->one();

        if (Yii::$app->request->isPost) {
            if ($model->updateEmail(Yii::$app->request->post())) {
                Yii::$app->session->setFlash('info', '修改成功');
            } else {
                Yii::$app->session->setFlash('info', '修改成功');
            }
        }

        $model->adminpass = '';
        return $this->render('changemail', [
            'model' => $model,
        ]);
    }

    public function actionChangePass()
    {
        $model = Admin::find()->where(['adminuser' => Yii::$app->session['admin']['adminuser']])->one();
        if (Yii::$app->request->isPost) {
            if ($model->updatePass(Yii::$app->request->post())) {
                Yii::$app->session->setFlash('info', '修改成功');
            } else {
                Yii::$app->session->setFlash('info', '修改失败');

            }
        }

        $model->adminpass = '';
        $model->repass    = '';
        return $this->render('changePass', [
            'model' => $model,
        ]);
    }

}